package cn.itcast.jsonParse;


import cn.itcast.beans.VehiclePlusBean;
import org.json.JSONArray;
import org.json.JSONObject;

import java.util.ArrayList;
import java.util.List;

/**
 * @author lilulu
 * @date 2023-04-16 18:41
 */
public class JsonParsePlusTest {

    public static void main(String[] args) {
        /**
         * 实现步骤：
         * 1）创建json字符串
         * 2）创建java类
         * 3）将使用org.json解析json字符串
         * 4）将json的字符串解析后为key：vaue，赋值给javaBean
         * 5）打印测试
         */
        String jsonStr = "{\"batteryAlarm\": 0,\"carMode\": 1,\"minVoltageBattery\": 3.89,\"chargeStatus\": 1,\"vin\": \"LS5A3CJC0JF890971\",\"nevChargeSystemTemperatureDtoList\": [{\"probeTemperatures\": [25, 23, 24, 21, 24, 21, 23, 21, 23, 21, 24, 21, 24, 21, 25, 21],\"chargeTemperatureProbeNum\": 16,\"childSystemNum\": 1}]}";
        VehiclePlusBean vehiclePlusBean = new VehiclePlusBean();
        JSONObject jsonObject = new JSONObject(jsonStr);
        int batteryAlarm = jsonObject.getInt("batteryAlarm");
        int carMode = jsonObject.getInt("carMode");
        double minVoltageBattery = jsonObject.getDouble("minVoltageBattery");
        int chargeStatus = jsonObject.getInt("chargeStatus");
        String vin = jsonObject.getString("vin");
        final JSONArray nevChargeSystemTemperatureDtoList = jsonObject.getJSONArray("nevChargeSystemTemperatureDtoList");
        final String firstArrJson = nevChargeSystemTemperatureDtoList.get(0).toString();
        final JSONObject nevChargeSystemTemperatureDtoObj = new JSONObject(firstArrJson);
        int chargeTemperatureProbeNum = nevChargeSystemTemperatureDtoObj.getInt("chargeTemperatureProbeNum");
        int childSystemNum = nevChargeSystemTemperatureDtoObj.getInt("childSystemNum");
        final JSONArray probeTemperatures = nevChargeSystemTemperatureDtoObj.getJSONArray("probeTemperatures");
        List<Integer> list = new ArrayList<>();
        for (Object probeTemperature : probeTemperatures) {
            list.add(Integer.parseInt(probeTemperature.toString()));
        }
        vehiclePlusBean = new VehiclePlusBean(
                batteryAlarm,
                carMode,
                minVoltageBattery,
                chargeStatus,
                vin,
                list,
                chargeTemperatureProbeNum,
                childSystemNum
        );
        System.out.println(vehiclePlusBean);
    }


}
